//
//  ZCInvolvedDetailsVC.m
//  zctx
//
//  Created by apple on 15/10/12.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCInvolvedDetailsVC.h"
#import "ZCRaisePayVC.h"
#import "ZCInvolvedDeatailsProgressCell.h"
#import "ZCInvolvedDeatailsCell.h"
#import "UIPopoverListView.h"
#import "ZCLoginVC.h"
@interface ZCInvolvedDetailsVC ()

@end

@implementation ZCInvolvedDetailsVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    
    NSLog(@"pid = %@",self.pid);
    self.tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 560);
    
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    flowLayout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH,self.tableView.bounds.size.height);//头部
    [self.collectionView setCollectionViewLayout:flowLayout];
        
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCInvolvedDeatailsProgressCell" bundle:nil] forCellReuseIdentifier:@"involvedprogressCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCInvolvedDeatailsCell" bundle:nil] forCellReuseIdentifier:@"involveddetailCell"];
}

#pragma mark 请求支持者数据
-(void)requestSupporterdata{
    
    ProgressHUDStr;
    [NetworkManager requestFoundingGetOneSponsorWithId:self.pid Mid:UserModel.mid password:UserModel.password pagecount:@"0" success:^(NSDictionary *dictResponse) {
        ProgressDismiss;
        sponsorArray = [[dictResponse objectForKey:@"content"] mutableCopy];
        [self.collectionView reloadData];        
        NSIndexPath *indexSet=[NSIndexPath indexPathForItem:0 inSection:1];
        ZCInvolvedDeatailsProgressCell *cell = [self.tableView cellForRowAtIndexPath:indexSet];
        cell.countLable.text =[NSString stringWithFormat:@"%lu人已支持",(unsigned long)sponsorArray.count];
        [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexSet] withRowAnimation:UITableViewRowAnimationNone];
    } failure:^(NSError *error) {
        ProgressDismiss;
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ZCProjectdetailsModel *detailsmodel = [ZCProjectdetailsModel modelObjectWithDictionary:datadictionary];
    
    if (indexPath.section==0) {
        ZCprojectdetailinfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"info"];
        cell.detailesModel = detailsmodel;
        return cell;
    }else if (indexPath.section==1){
        ZCInvolvedDeatailsProgressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"involvedprogressCell"];
        cell.detailModel = detailsmodel;
        cell.countLable.text = [NSString stringWithFormat:@"%lu人已支持",(unsigned long)sponsorArray.count];
        return cell;
    }else if(indexPath.section==2){
        ZCInvolvedDeatailsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"involveddetailCell"];
        __block ZCInvolvedDetailsVC *weekself = self;
        cell.model = detailsmodel;
        cell.cellButton = ^(){
            if (UserModel.password.length>1) {
                [weekself popClickAction];
            }else{
                ZCLoginVC *login = [[ZCLoginVC alloc]init];
                UINavigationController *nalogin = [[UINavigationController alloc]initWithRootViewController:login];
                [weekself presentViewController:nalogin animated:YES completion:nil];
            }
            
        };
        
        return cell;
    }else{
        ZCProjectdetailSamplingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"sampling"];
        [cell.contentView removeFromSuperview];
        return cell;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        return 148;
    }else if (indexPath.section==1){
        return 100;
    }else if(indexPath.section==2){
        return 100;
    }else{
        return 10;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    NSArray *titleArray = [NSArray arrayWithObjects:@"基本信息",@"项目进度",@"众筹信息",@"支持者",nil];
    UIView * headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    headView.backgroundColor = [UIColor whiteColor];
    UIView *bgheadView = [UIView new];
    [headView addSubview:bgheadView];
    bgheadView.backgroundColor = [UIColor colorWithHexString:@"#ebeded"];
    [bgheadView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headView.top).with.offset(0);
        make.left.equalTo(headView).with.offset(0);
        make.right.equalTo(headView).with.offset(0);
        make.height.mas_equalTo(@10);
    }];
    
    UIView *newView = [UIView new];
    [headView addSubview:newView];
    [newView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headView.top).with.offset(10);
        make.left.equalTo(headView).with.offset(0);
        make.right.equalTo(headView).with.offset(0);
        make.bottom.equalTo(headView).with.offset(0);
    }];
    
    UILabel *leftlable = [UILabel new];
    [newView addSubview:leftlable];
    leftlable.font = [UIFont systemFontOfSize:16];
    [leftlable setText:titleArray[section]];
    //        [leftlable setText:@"最新检测结果"];
    [leftlable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(newView.mas_centerY);
        make.left.equalTo(newView).with.offset(15);
        make.height.mas_equalTo(@30);
        make.width.mas_equalTo(@150);
    }];
    
    if (section==1) {
        UILabel *statelable = [UILabel new];
        [newView addSubview:statelable];
        statelable.font = [UIFont systemFontOfSize:16];
        statelable.textColor = MainColor;
        [statelable setText:@"筹资中"];
        [statelable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(newView.mas_centerY);
            make.right.equalTo(newView).with.offset(-15);
            make.height.mas_equalTo(@30);
            //            make.width.mas_equalTo(@150);
        }];
        
    }
    return headView;
    
}
#pragma mark 立即支持

- (void)popClickAction
{
    CGFloat xWidth = SCREEN_WIDTH - 80.0f;
    CGFloat yHeight = 272.0f;
    CGFloat yOffset = (self.view.bounds.size.height - yHeight)/2.0f-50;
    UIPopoverListView *poplistview = [[UIPopoverListView alloc] initWithFrame:CGRectMake(10, yOffset, xWidth, yHeight)];
    
    __block ZCInvolvedDetailsVC *weekself = self;
    poplistview.moneyButtonClik = ^(NSInteger index,NSString *titleStr){
        NSLog(@"index = %ld , titleStr = %@",(long)index,titleStr);
        ZCRaisePayVC *zcVC= [[ZCRaisePayVC alloc]init];
        zcVC.hidesBottomBarWhenPushed = YES;
        zcVC.index = index;
        zcVC.priceStr = titleStr;
        zcVC.pid = self.pid;
        [weekself.navigationController pushViewController:zcVC animated:YES];
        
    };
    [poplistview show];
}
@end
